<%--
  Created by IntelliJ IDEA.
  User: Admin
  Date: 2022/8/11
  Time: 16:36
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
  <title>Title</title>
  <script src="js/jquery-3.6.0.js"></script>
</head>
<body>
<form action="servletInsert" method="post">
  <table border="1">
    <tr>
      <td>商品名称:</td>
      <td><input type="text" name="productName" id="user"></td>
    </tr>
    <tr>
      <td>商品描述:</td>
      <td>
                <textarea id="desc" name="producDesc" >

                 </textarea>
      </td>
    </tr>
    <tr>
      <td>商品价格:</td>
      <td><input type="text" name="producPrice" id="price" ></td>
    </tr>
    <tr>
      <td>商品状态:</td>
      <td>
        <input type="radio" name="state" checked value="1" title="上架">
        <input type="radio" name="state" value="0" title="下架">
      </td>
    </tr>
    <tr>
      <td>操作:</td>
      <td>
        <input type="submit" value="新增">
        <input type="reset">
      </td>
    </tr>
  </table>
</form>
<script>
  $(function (){
    $("form").submit(function (){
      if($("#user").val().trim()===""){
        alert('商品名称不能为空')
        return false;
      }
      if($("#desc").val().trim()===""){
        alert('商品描述不能为空')
        return false;
      }
      var p =/^[1-9]{1,2}$|^0\.\d{1,2}$|^[1-9]\d{1,2}\.\d{2}$/
      var pri  =$("#price").val().trim();
      if (isNaN(pri)){
        alert("商品价格必须是数值")
        return false;
      }
      if (p.test(pri)) {
        alert('商品价格必须保留两位小数')
        return false;
      }
      return true;
    })
  })
</script>
</body>
</html>

